//
//  UIImage+Merge.m
//  iOS-Categories (https://github.com/shaojiankui/iOS-Categories)
//
//  Created by Jakey on 14/12/30.
//  Copyright (c) 2014年 www.skyfox.org. All rights reserved.
//

#import "UIImage+Merge.h"

@implementation UIImage (Merge)
+ (UIImage*)mergeImage:(UIImage*)firstImage withImage:(UIImage*)secondImage {
    return [self mergeImage:firstImage withImage:secondImage offset:CGPointZero];
}

+ (UIImage*)mergeImage:(UIImage*)firstImage withImage:(UIImage*)secondImage offset:(CGPoint)offset{
    CGImageRef firstImageRef = firstImage.CGImage;
    CGFloat firstWidth = CGImageGetWidth(firstImageRef);
    CGFloat firstHeight = CGImageGetHeight(firstImageRef);
    CGImageRef secondImageRef = secondImage.CGImage;
    CGFloat secondWidth = CGImageGetWidth(secondImageRef);
    CGFloat secondHeight = CGImageGetHeight(secondImageRef);
    CGSize mergedSize = CGSizeMake(MAX(firstWidth, secondWidth), MAX(firstHeight, secondHeight));
    UIGraphicsBeginImageContext(mergedSize);
    // 第一个位置不变
    [firstImage drawInRect:CGRectMake(0, 0, firstWidth, firstHeight)];
    
    // 第二个判断偏移量
    [secondImage drawInRect:CGRectMake(offset.x*([UIScreen mainScreen].scale), offset.y*([UIScreen mainScreen].scale), secondWidth, secondHeight)];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
@end
